package com.liwei.toc.service;

import java.sql.SQLException;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.liwei.toc.data.IbatisSQLMapConfig;
import com.liwei.toc.domain.TocDoaj;

public class DoajTocMain implements Runnable {

	private static SqlMapClient sqlMapper;
	String url = "http://www.doaj.org/doaj?func=abstract&id=";

	private int index;

	public DoajTocMain(int index) {
		this.index = index;
	}

	public static void main(String[] args) {
		for (int index = 0; index < 1; index++) {
			Thread t = new Thread(new DoajTocMain(index));
			t.start();
		}
	}

	public void run() {
		sqlMapper = IbatisSQLMapConfig.getSqlMapInstance();
		int i = 30089;
		// for (int i = 5000 * index; i < 5000 * (index + 1); i++) {
		try {
			TocDoaj td = parse(url + i, i);
			if (td.getTitle() != null) {
				if (!td.getTitle().equals("")) {
					td.setUrl(url + i);
					DoajTocMain.insertTocDoaj(td);
				}
			}
			System.out.println(i);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// }

	}

	public TocDoaj parse(String url, int index) {

		String content = null;

		content = getWebContent.getContent(url, "UTF-8");
		DoajParser dp = new DoajParser();
		return dp.parse(content, index);

	}

	public static void insertTocDoaj(TocDoaj tocDoaj) throws SQLException {
		sqlMapper.insert("insertTocDoaj", tocDoaj);
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}
}
